/**
 * 文件名    ： ResourceGain.java
 * 版权       : XX科技有限公司。
 * 描述       : <描述>
 * 修改时间：2019年9月4日
 * 修改内容：<修改内容>
 */
package com.example.demo.application.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(description = "资源查询")
@RestController
@EnableAutoConfiguration
@RequestMapping(path = "/resourcesearch")
public class ResourceSearch {

	@ApiOperation(value = "资源查询", notes = "资源查询")
	@RequestMapping(path = "/nodeinfo", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> nodeinfo(String shell) throws Exception {
		String result = "";
		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("availableProcessors", Runtime.getRuntime().availableProcessors());
		modelMap.put("maxMemory", Runtime.getRuntime().maxMemory());
		// 是否G1 回收
		String command = "java -XX:+PrintFlagsFinal -version |grep UseG1GC | awk '{print $4}'";
		modelMap.put("UseG1GC", execshell(command));
		// 是否并发回收
		command = "java -XX:+PrintFlagsFinal -version |grep UseParallelGC  | awk '{print $4}'";
		modelMap.put("UseParallelGC", execshell(command));
		// 并发回收线程数
		command = "java -XX:+PrintFlagsFinal -version |grep ParallelGCThreads  | awk '{print $4}'";
		modelMap.put("ParallelGCThreads", execshell(command));
		// 回收线程的线程堆大小
		command = "java -XX:+PrintFlagsFinal -version |grep HeapSizePerGCThread | awk '{print $4}'";
		result = execshell(command);
		if (StringUtils.isNotBlank(result) && StringUtils.isNumericSpace(result)) {
			result = result.replaceAll(" ", "");
			modelMap.put("HeapSizePerGCThread", Long.parseLong(result) / 1024 / 1024);
		}
		modelMap.put("HeapSizePerGCThreadStr", result);

		// java虚拟机最大内存
		command = "java -XX:+PrintFlagsFinal -version |grep MaxHeapSize | awk '{print $4}'";
		result = execshell(command);
		if (StringUtils.isNotBlank(result) && StringUtils.isNumericSpace(result)) {
			result = result.replaceAll(" ", "");
			modelMap.put("MaxHeapSize", Long.parseLong(result) / 1024 / 1024);
		}
		modelMap.put("MaxHeapSizeStr", result);

		modelMap.put("code", "1");
		return modelMap;

	}

	private String execshell(String mcommand) throws IOException {
		Process process;
		String line = null;
		StringBuilder sb = new StringBuilder();
		try {
			process = Runtime.getRuntime().exec(mcommand);
			BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
			while ((line = reader.readLine()) != null) {
				sb.append(line).append("\r\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return sb.toString();
	}

	public static void main(String[] args) throws IOException {
		ResourceSearch rs = new ResourceSearch();
		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("availableProcessors", Runtime.getRuntime().availableProcessors());
		modelMap.put("maxMemory", Runtime.getRuntime().maxMemory());
		// 是否G1 回收
		String command = "java -XX:+PrintFlagsFinal -version |grep UseG1GC | awk '{print $4}'";
		// 是否并发回收
		command = "java -XX:+PrintFlagsFinal -version |grep UseParallelGC  | awk '{print $4}'";
		// 并发回收线程数
		command = "java -XX:+PrintFlagsFinal -version |grep ParallelGCThreads  | awk '{print $4}'";
		// 回收线程的线程堆大小
		command = "java -XX:+PrintFlagsFinal -version |grep HeapSizePerGCThread | awk '{print $4}'";
		// java虚拟机最大内存
		command = "java -XX:+PrintFlagsFinal -version |grep MaxHeapSize | awk '{print $4}'";
		// 回收线程的线程堆大小
		command = "java -XX:+PrintFlagsFinal -version |grep HeapSizePerGCThread | awk '{print $4}'";
		System.out.println(rs.execshell(command));
		
	}

}
